import panel, { panel_zindex } from '../../scripts/orc/panel/panel';
import UI_panel_loading from './package_loading/UI_panel_loading';
import package_loading_Binder from './package_loading/package_loading_Binder';
import PanelManager from '../../scripts/orc/PanelManager';
import PanelDefine from '../../scripts/orc/PanelDefine';
const { ccclass, property } = cc._decorator;
@ccclass
export default class panel_loading extends panel.panel {
    package_name: string = 'package_loading';
    panel_name: string = 'panel_loading';
    server_data: any = {};             //初始数据
    zindex: string = panel_zindex.panelLayer;
    _view: UI_panel_loading;
    _pview: fgui.GComponent;

    loading_per: number = 0;    //保证进度条不会回滚
    creatUI() {
        package_loading_Binder.bindAll();
        this._view = UI_panel_loading.createInstance();
        this._view.makeFullScreen();
        this._view.name = 'ui';
        this._view.node.name = 'ui';
        this._pview.addChild(this._view);
    }


    onLoad() {
        this.creatUI();
    }

    start() {
        console.log('panel start');
        this._view.m_per.value = 0;
        cc.assetManager.loadBundle('common', (err, bundle) => {
            if (!err) {
                console.log('bundle:', bundle);
                //先加载包
                fgui.UIPackage.loadPackage(bundle, 'common', loading_callback, (error, assets) => {
                    console.log('finish');
                    this.close();
                    PanelManager.loadPanel(PanelDefine.PANEL_ID_1002, 'hall');
                });

            }
        });

        var loading_callback = (completedCount, totalCount, item) => {
            console.log("completedCount:" + completedCount + ",totalCount:" + totalCount);
            this.loading_per = (completedCount / totalCount * 100) > this.loading_per ? (completedCount / totalCount * 100) : this.loading_per;
            this._view.m_per.value = this.loading_per;
        };
    }


}